import {
  getSetting, chooseAddress, openSetting, showModal, showToast
} from '../../utlis/asyncWx';
// ES7兼容插件
// import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    address: {}, // 购物地址
    cart: [],//购物车列表
    allCheck: false,//是否全选
    totalPrice: 0,//总价
    totalNum: 0//总件数
  },
  // 点击结算
  async handleToPay() {
    if (!this.data.address.userName) {
      await showToast({ title: '请添加地址' })
      return
    }
    if (this.data.totalNum === 0) {
      await showToast({ title: '请选择商品' })
      return
    }
    wx.navigateTo({
      url: '/pages/pay/pay',
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });
  },
  // 监听页面显示
  onShow: function () {
    // 获取地址
    let address = wx.getStorageSync('address')
    this.setData({
      address
    })
    // 更新购物车
    let cart = wx.getStorageSync('cart') || []
    // 调用
    this.setCart(cart)
  },
  // 勾选改变
  handleItemChange(e) {
    const goods_id = e.currentTarget.dataset.id
    // console.log(goods_id);
    let { cart } = this.data
    let index = cart.findIndex(v => v.goods_id === goods_id)
    cart[index].check = !cart[index].check    // 取反
    wx.setStorageSync("cart", cart)
    // 调用
    this.setCart(cart)
  },
  // 获取地址
  async handleChooseAddress() {
    console.log('收货地址');
    try {
      // 获取权限状态
      const res1 = await getSetting()
      console.log(res1);
      const scopeAddress = res1.authSetting["scope.address"];
      //  如果用户曾经获取权限时点过取消,让他打开设置设置
      if (scopeAddress === false) {
        // 打开设置
        await openSetting()
      }
      // 如果用授予权限时点过确定或者从来没点过,
      let address = await chooseAddress()
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
      console.log(address);
      wx.setStorageSync("address", address)
      this.setData({
        address
      })
    } catch (err) {
      console.error(err)
    }
  },
  onLoad: function (options) {
  },
  async editNum(e) {
    const { operation, id } = e.currentTarget.dataset
    let { cart } = this.data
    const index = cart.findIndex(v => v.goods_id == id)
    // console.log(index);
    if (cart[index].num === 1 && operation === -1) {
      const result = await showModal({ content: '确定删除?' })
      if (result.confirm) {
        console.log('拜你个拜');
        cart.splice(index, 1)
        this.setData({ cart })
        this.setCart(cart)
      } else if (result.cancel) {
        console.log('那没事了');
      }
    } else {
      console.log(cart[index].num);
      cart[index].num += operation
      this.setCart(cart)
    }
  },
  // 点击全选
  handleALlCheck() {
    let { cart, allCheck } = this.data
    // 取反
    allCheck = !allCheck
    cart.forEach(v => v.check = allCheck)
    // console.log(cart);
    // 调用
    this.setCart(cart)
  },
  // 设置购物车状态: 总数 总价 勾选
  setCart(cart) {
    // 选中,总价,总数
    let allCheck = true, totalPrice = 0, totalNum = 0
    //cart是遍历的数据数组,cart.check有值,true或者false
    //every返回布尔值,空数组.every返回true
    // const allCheck = cart.length === 0 ? false : cart.every(v => v.check)
    // 遍历
    cart.forEach(v => {
      if (v.check) {
        totalPrice += v.num * v.goods_price
        totalNum += v.num
      } else {
        allCheck = false
      }
    })
    // 优化bug
    allCheck = cart.length != 0 ? allCheck : false;
    wx.setStorageSync("cart", cart)
    // 提交数据
    this.setData({
      cart,
      allCheck,
      totalPrice,
      totalNum
    })
  }
});